//
//  BMSelectedTextField.m
//  bigmama
//
//  Created by huyuchen on 2017/8/14.
//  Copyright © 2017年 盛成文化. All rights reserved.
//

#import "BMSelectedTextField.h"

@interface BMSelectedTextField ()
@property (nonatomic, copy) SetupPickerViewBlock setupPickerViewBlock;
@property (nonatomic, copy) void(^SetupToolBarBlock)(BMPickerViewToolBar *toolBar);
@property (nonatomic, strong) NSDateFormatter *formatter;
@end

@implementation BMSelectedTextField

// 设置toolBar
- (void)setupToolBarWithBlock:(void(^)(BMPickerViewToolBar *toolBar))block{
    self.SetupToolBarBlock = [block copy];
}


- (void)showSingleColPickerWithToolBarText:(NSString *)toolBarText data:(NSArray<NSString *> *)data defaultIndex:(NSInteger)defaultIndex toolBarStyle:(BMPickerViewToolBarStyle)toolBarStyle cancelBlock:(CancelBlock)cancelBlock clickBlock:(TextFieldSingleClickBlock)clickBlock{
    
    __weak typeof(self) weakSelf = self;
    self.setupPickerViewBlock = ^UIView *{
        BMSinglePickerView *single = [[BMSinglePickerView alloc] initWithToolBarText:toolBarText defaultIndex:defaultIndex data:data toolBarStyle:toolBarStyle valueDidChangedBlock:^(NSString *selectedValue) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (strongSelf) {
                strongSelf.text = selectedValue;
            }
        } cancelBlock:^{
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (strongSelf) {
                [strongSelf endEditing:YES];
                if (cancelBlock) {
                    cancelBlock();
                }
            }
        } doneBlock:^(NSInteger selectedIndex, NSString *selectedValue) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (strongSelf) {
                [strongSelf endEditing:YES];
                if (clickBlock) {
                    clickBlock(strongSelf,selectedIndex,selectedValue);
                }
            }
        }];
        
        return single;
    };
}


- (void)showMultipleColPickerWithToolBarText:(NSString *)toolBarText defaultIndexs: (NSArray *)defaultIndexs data:(NSArray<NSArray<NSString *> *> *)data toolBarStyle:(BMPickerViewToolBarStyle)toolBarStyle cancelBlock:(CancelBlock)cancelBlock clickBlock: (TextFieldMulpitleClickBlock)clickBlock{
    
    __weak typeof(self) weakSelf = self;
    
    self.setupPickerViewBlock = ^UIView *{
        BMMultiplePickerView *multiple = [[BMMultiplePickerView alloc] initWithToolBarText:toolBarText defaultIndexs:defaultIndexs data:data toolBarStyle:toolBarStyle valueDidChangedBlock:^(NSArray *selectedValues) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (strongSelf) {
                NSString *tempString = @"";
                for (NSString *value in selectedValues) {
                    // 这里加了两个空格
                    tempString = [tempString stringByAppendingString:[NSString stringWithFormat:@"  %@",value]];
                }
                strongSelf.text = tempString;
            }
        } cancelBlock:^{
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (strongSelf) {
                [strongSelf endEditing:YES];
                if (cancelBlock) {
                    cancelBlock();
                }
            }
        } doneBlock:^(NSArray *selectedIndexs, NSArray *selectedValues) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (strongSelf) {
                [strongSelf endEditing:YES];
                if (clickBlock) {
                    clickBlock(strongSelf,selectedIndexs,selectedValues);
                }
            }
        }];
        
        return multiple;
    };
}


- (void)showMultipleAssociatedColPickerWithToolBarText:(NSString *)toolBarText defaultValues:(NSArray *)defaultValues data:(NSArray *)data toolBarStyle:(BMPickerViewToolBarStyle)toolBarStyle cancelBlock:(CancelBlock)cancelBlock clickBlock: (TextFieldMulpitleAssociatedClickBlock)clickBlock{
    
    __weak typeof(self) weakSelf = self;
    
    self.setupPickerViewBlock = ^UIView *{
        BMMultipleAssociatedPickerView *assoMultiple = [[BMMultipleAssociatedPickerView alloc] initWithToolBarText:toolBarText defaultValues:defaultValues data:data toolBarStyle:toolBarStyle valueDidChangedHandler:^(NSArray *selectedValues) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (strongSelf) {
                NSString *tempString = @"";
                for (NSString *value in selectedValues) {
                    // 这里加了两个空格
                    tempString = [tempString stringByAppendingString:[NSString stringWithFormat:@"  %@",value]];
                }
                strongSelf.text = tempString;
            }
        } cancelBlock:^{
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (strongSelf) {
                [strongSelf endEditing:YES];
                if (cancelBlock) {
                    cancelBlock();
                }
            }
        } doneBlock:^(NSArray *selectedValues) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (strongSelf) {
                [strongSelf endEditing:YES];
                if (clickBlock) {
                    clickBlock(strongSelf,selectedValues);
                }
            }
        }];
        
        return assoMultiple;
    };
}


- (void)showDatePickerWithToolBarText:(NSString *)toolBarText style:(BMDatePickerViewStyle *)style toolBarStyle:(BMPickerViewToolBarStyle)toolBarStyle cancelBlock:(CancelBlock)cancelBlock clickBlock:(TextFieldDateClickBlock)clickBlock{
    
    __weak typeof(self) weakSelf = self;
    
    self.setupPickerViewBlock = ^UIView *{
        BMNewDatePickerView *pickerView = [[BMNewDatePickerView alloc] initWithToolBarText:toolBarText pickerViewStyle:style toolBarStyle:toolBarStyle valueDidChangedBlock:^(NSDate *selectedDate) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (strongSelf) {
                strongSelf.text = [strongSelf.formatter stringFromDate:selectedDate];
            }
        } cancelBlock:^{
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (strongSelf) {
                [strongSelf endEditing:YES];
                if (cancelBlock) {
                    cancelBlock();
                }
                
            }
        } doneBlock:^(NSDate *selectedDate) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (strongSelf) {
                [strongSelf endEditing:YES];
                if (clickBlock) {
                    clickBlock(strongSelf, selectedDate);
                }
                
            }
        }];
        
        return pickerView;
    };
}

#pragma mark - life cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupInit];
    }
    return self;
}

- (instancetype)init {
    if (self = [super init]) {
        [self setupInit];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setupInit];
    }
    return self;
}

- (void)setupInit{
    _autoSetSelectedText = NO;
    _pickerViewHeight = 260;
    
    NSNotificationCenter *noCenter = [NSNotificationCenter defaultCenter];
    [noCenter addObserver:self selector:@selector(selectedTextFieldDidBeginEdit) name:UITextFieldTextDidBeginEditingNotification object:self];
    [noCenter addObserver:self selector:@selector(selectedTextFieldDidEndEdit) name:UITextFieldTextDidEndEditingNotification object:self];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    NSLog(@"BMSelectedTextField dealloc");
}

#pragma mark - helper
// 开始编辑时才添加pickerView
- (void)selectedTextFieldDidBeginEdit {
//    if (self.setupPickerViewBlock) {
    
        if (self.setupPickerViewBlock) {
            
            UIView *pickerView  = self.setupPickerViewBlock();
            pickerView.frame = CGRectMake(0, 0, 0, _pickerViewHeight);
            self.inputView = pickerView;
            
            if (self.SetupToolBarBlock) {
                BMPickerViewToolBar *toolBar = [self getToolBarFromPickerView:pickerView];
                self.SetupToolBarBlock(toolBar);
            }
            
        }
        
//    }
}

- (BMPickerViewToolBar *)getToolBarFromPickerView:(UIView *)pickerView {
    if ([pickerView isKindOfClass:[BMSinglePickerView class]]) {
        return ((BMSinglePickerView *)pickerView).toolBar;
    }
    else if ([pickerView isKindOfClass:[BMMultiplePickerView class]]) {
        return ((BMMultiplePickerView *)pickerView).toolBar;
    }
    else if ([pickerView isKindOfClass:[BMMultipleAssociatedPickerView class]]) {
        return ((BMMultipleAssociatedPickerView *)pickerView).toolBar;
    }
    else if ([pickerView isKindOfClass:[BMNewDatePickerView class]]) {
        return ((BMNewDatePickerView *)pickerView).toolBar;
    }
    else {
        return [BMPickerViewToolBar new];
    }
    
}

// 编辑完成销毁pickerView
- (void)selectedTextFieldDidEndEdit {
    self.inputView = nil;
}

// 不显示输入光标
- (CGRect)caretRectForPosition:(UITextPosition *)position {
    return CGRectZero;
}

// 这是一个耗时操作...
- (NSDateFormatter *)formatter {
    if (!_formatter) {
        _formatter = [[NSDateFormatter alloc] init];
        _formatter.dateFormat = @"yyyy-MM-dd";
    }
    return _formatter;
}

@end
